package com.codergd.gra.alipay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.codergd.gra.config.Mesg;
import org.springframework.stereotype.Component;

/**
 * 交易查询
 */
@Component
public class AlipayTradeQuery {
//    public AlipayTradeQuery() throws AlipayApiException {
//
//        AlipayClient alipayClient = new DefaultAlipayClient(Mesg.URL,Mesg.APPID,
//                Mesg.APP_PRIVATE_KEY,"json","GBK",
//                Mesg.ALIPAY_PUBLIC_KEY,"RSA2");
//
//        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//
//        JSONObject bizContent = new JSONObject();
//        bizContent.put("out_trade_no", "202108170101010638");
//
//        request.setBizContent(bizContent.toString());
//        AlipayTradeQueryResponse response = alipayClient.execute(request);
//        if(response.isSuccess()){
//            System.out.println("调用成功");
//        }else{
//            System.out.println("调用失败");
//        }
//        System.out.println(response.getBody());
//    }

    public String queryState(String no) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(Mesg.URL,Mesg.APPID,
                Mesg.APP_PRIVATE_KEY,"json","GBK",
                Mesg.ALIPAY_PUBLIC_KEY,"RSA2");

        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", no);

        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        }else{
            System.out.println("调用失败");
        }
//        System.out.println(response.getBody());
        return response.getMsg();
    }
}
